<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: relative;
            width: 200px;
            height: 200px;
            background-color: pink;
            margin: 100px;
        }

        p {
            width: 100px;
            height: 100px;
            background-color: blueviolet;
            margin: 50px;
        }
    </style>
</head>

<body>
    <div>
        <p></p>
    </div>
    <script>
        const div = document.querySelector('div')
        const p = document.querySelector('p')
        console.log(div.offsetLeft);//body自己有个8px的外边距 有了这个很方便
        console.log(p.offsetLeft);
    </script>
</body>

</html>